<?php

class BetController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	$this->_helper->acl->allow('user', 'index');
    }

    public function indexAction()
    {
    	/*
        $request = $this->getRequest();
        $event_tbl = new Application_Model_DbTable_Event();
        $bet_tbl = new Application_Model_DbTable_Bet();
    	$empty = new Zend_Validate_NotEmpty(array('integer'));
    	$validators = array('id' => array('Int', $empty));
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($request->getParams());
	    if($input->isValid()){	    	
	    	if($event_tbl->CheckDate($input->id)){	    		
	    		$form = new Application_Form_Bet();	    		
	    		$event = $event_tbl->GetEventsById($input->id)->current();
	    		$this->view->form = $form;
	    		$this->view->event = $event;
	    		$user_id = $this->_helper->Logged->getUserId();
	    		if($bet_tbl->CheckBetExists($input->id, $user_id)){
	    			$bet = $bet_tbl->GetBetsById(null, $user_id, $input->id);
    				$this->view->form->populate($bet->current()->toArray());	    		
	    		}
	    	    if($request->isPost()){
	    	    	if($form->isValid($request->getPost())){
	    	    		$bet_tbl->SaveBet($input->id, $user_id, $form->getValue('bet_left'), $form->getValue('bet_right'));
	    	    		$messenger = $this->_helper->FlashMessenger;
						$messenger->addMessage('You just successfully BET');
						$this->_helper->redirector('success', 'index');
	    	    	}    		    		
    			}
	    	}
	    	else{
	    		$this->view->message = 'Sorry, but bet date of this event is expired';
	    	}
	    }
	    else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }
	    */
    	$this->view->title = 'Bet';
		$this->view->headTitle($this->view->title, 'PREPEND');
		if($input = $this->_checkInput('id', 'event')){	 
			$event = new Application_Model_Event($input->id);
			if($event->CheckDate()){
				$form = new Application_Form_AdminBet();
				$tl = $event->getTeamL();
				$tr = $event->getTeamR();
				$form->removeElement('status');
				$form->bet_left->setLabel($tl['title']);
				$form->bet_right->setLabel($tr['title']);
				$form->submit->setLabel('Bet');
				$form->removeDecorator('Form');
				$form->addDecorator(new Application_Form_Decorator_Test());
				$this->view->form = $form;
				$bet = new Application_Model_Bet();
				$user_id = $this->_helper->Logged->getUserId();
				if($res = $bet->getBet($user_id, $input->id)){
					$form->populate($res->toArray());					
				}
				$request = $this->getRequest();
				if($request->isPost()){
					if($form->isValid($request->getPost())){						
						$bet->fill($form->getValues());
						$bet->user_id = $user_id;
						$bet->event_id = $input->id;
	    	    		$bet->recountBet();
	     	    		//$messenger = $this->_helper->FlashMessenger;
						//$messenger->addMessage('Bet edited');
						//$this->_helper->redirector(null, 'bet', 'default', array('id' => $input->id));
						$this->_redirect('event/' . $input->id);
					}
				}				
			}
			else{
				$this->view->title = 'Sorry, but bet date of this event is expired';
			}
		}
	    else{
	    	throw new Zend_Controller_Action_Exception('Invalid input');
	    }
    }
	private function _checkInput($param, $field = null){
		$validatorsSet = array();
		$validatorsSet[] = 'Int';
		$validatorsSet[] = new Zend_Validate_GreaterThan(-1);
		//$validatorsSet[] = new Zend_Validate_NotEmpty(Zend_Validate_NotEmpty::ALL);
		if($field){
			$validatorsSet[] = new Zend_Validate_Db_RecordExists(array('table' => $field, 'field' => $param));
		}
    	$validators = array($param => $validatorsSet);
	    $input = new Zend_Filter_Input(null, $validators);
	    $input->setData($this->getRequest()->getParams());
	    if($input->isValid()){
	    	return $input;
	    }
	    else{
	    	return false;
	    }		
	}
}